package com.zhihuichongcheng.application.ui.im.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.smartlib.cmnObject.recycleView.OnItemOnclick;
import com.smartlib.cmnObject.util.DateUtil;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.bean.im.AnnouncementType;
import com.zhihuichongcheng.application.bean.im.NoticeBean;
import com.zhihuichongcheng.application.glide.GlideCircleTransform;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 3water on 2016/10/27.
 */

public class NoticeAdapter extends RecyclerView.Adapter<NoticeAdapter.ViewHold> {
    private Context mContext;
    private List<NoticeBean> mListData = new ArrayList<>();
    private OnItemOnclick<NoticeBean> onclick;
    public NoticeAdapter(Context context)
    {
        this.mContext = context;
    }
    @Override
    public ViewHold onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.listadapter_notice,null);
        return new ViewHold(view);
    }

    public  void  setOnItemCLick(OnItemOnclick<NoticeBean> onItemCLick)
    {
        this.onclick = onItemCLick;
    }


    @Override
    public void onBindViewHolder(ViewHold holder, final int position) {
        final NoticeBean bean = mListData.get(position);
        Glide.with(mContext).load(R.drawable.ic_chat_notice_avatar).transform(new GlideCircleTransform(mContext)).into(holder.mImavatar);
        holder.mTvNoticeTitle.setText(bean.getTitle());
        holder.mTvNoticeContent.setText(bean.getSummary());
        AnnouncementType type = bean.getAnnouncementType();
        if(type!=null)
        {
            holder.mTvNoticeType.setText(type.getTypeName());
            holder.mTvNoticeType.setVisibility(View.VISIBLE);
        }
        String metting = bean.getMeetingAnnouncementId();
        if(!TextUtils.isEmpty(metting))
        {
            Glide.with(mContext).load(R.drawable.ic_chat_mettingnotice).transform(new GlideCircleTransform(mContext)).into(holder.mImavatar);
            holder.mTvNoticeType.setText("会议公告");
            holder.mTvNoticeType.setVisibility(View.VISIBLE);
        }

        boolean read = bean.isRead();
        if(read)
        {
            holder.mImPoint.setVisibility(View.GONE);
        }
        else
        {
            holder.mImPoint.setVisibility(View.VISIBLE);
        }
        if(position==mListData.size()-1)
        {
            holder.mViewBottom.setVisibility(View.INVISIBLE);
        }
        else
        {
            holder.mViewBottom.setVisibility(View.VISIBLE);
        }

        Date date = DateUtil.parseStringToDate(bean.getCreateTime(),"yyyy年MM月dd日 HH:mm:ss");
        holder.mTvTime.setText(DateUtil.format(date));
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onclick!=null)
                {
                    onclick.onClick(bean,position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mListData.size();
    }

    public void setData(List<NoticeBean> list) {
        this.mListData.addAll(list);
    }

    public void setDataRead(int position) {
        if (mListData.size() > 0) {
            NoticeBean bean = mListData.get(position);
            bean.setRead(true);
            mListData.set(position, bean);
            notifyDataSetChanged();
        }

    }

    public void clear() {
        mListData.clear();
    }

    class ViewHold extends RecyclerView.ViewHolder{
        private ImageView mImavatar,mImPoint;
        private View view,mViewBottom;
        private TextView mTvNoticeTitle,mTvNoticeContent,mTvNoticeType,mTvTime;
        public ViewHold(View itemView) {
            super(itemView);
            view = itemView;
            mImPoint = (ImageView)itemView.findViewById(R.id.im_point);
            mImavatar = (ImageView)itemView.findViewById(R.id.im_avatar);
            mTvNoticeTitle = (TextView)itemView.findViewById(R.id.tv_notice_title);
            mTvNoticeContent = (TextView)itemView.findViewById(R.id.tv_notice_content);
            mTvNoticeType = (TextView)itemView.findViewById(R.id.tv_notice_type);
            mTvTime = (TextView)itemView.findViewById(R.id.tv_time);
            mViewBottom = (View)itemView.findViewById(R.id.view_bottom);
        }
    }

}
